package simulator;

import java.util.HashSet;
import java.util.Set;

public class Net {

	Set<Pc> pcs;
	String name;
	

	public Net() {
		this.pcs = new HashSet<Pc>();
	}
	
	public Set<Pc> getPcs() {
		return this.pcs;
	}
	
	public void addPc(Pc pc) {
		if(!this.pcs.contains(pc)) {
			this.pcs.add(pc);
		}
	}
	
	public Pc createPc(String pcName) {
		Pc pc = new Pc(pcName);
		this.pcs.add(pc);
		return pc;
	}
	
	public Pc getPc(String name) {
		for(Pc pc : this.pcs) {
			if(pc.getName().equals(name)) {
				return pc;
			}
		}
		return null;
	}
	
	public String getPcsNames() {
		StringBuilder names = new StringBuilder();
		names.append("Net pcs: ");
		for(Pc pc : pcs) {
			names.append(pc.getName() + ", ");
		}
		return names.toString();
	}
	
	public String getStatesString() {
		StringBuilder str = new StringBuilder();
		for(Pc pc : this.pcs) {
			str.append(pc.getState().name());
			str.append(" ");
		}
		return str.toString();
	}

	public int cantSane() {
		int ret = 0;
		for(Pc pc: this.pcs){
			if(pc.getState().equals(State.Sane)){
				ret++;
			}
		}
		return ret;
	}
	@Override
	public String toString() {
		return name;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Net other = (Net) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
